package com.jfl.web.controller.monitor;

import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.jfl.annotation.Log;
import com.jfl.base.AbstractController;
import com.jfl.common.Constants;
import com.jfl.core.utils.ShiroUtils;
import com.jfl.enums.LogType;
import com.jfl.enums.Module;
import com.jfl.exception.BusinessException;
import com.jfl.model.SysLog;
import com.jfl.model.SysUser;
import com.jfl.monitor.SysLogService;
import com.jfl.utils.ConvertRequestUtil;
import com.jfl.utils.ConvertUtil;

/**
 * 日志模块
 * 
 * @author 赵炎
 * @version [V1.00, 2018年12月31日]
 * @since V1.00
 */
@Controller
@RequestMapping("${jfl.module.monitor}/log")
public class SysLogController extends AbstractController
{
    @Reference(version = "${jfl.version}")
    private SysLogService sysLogService;
    
    @Value("${jfl.demoMode}")
    private boolean mode;
    
    /**
     * 日志页面
     * 
     * @param modelMap
     * @return
     */
    @RequiresPermissions("monitor:log:view")
    @GetMapping
    public String log(ModelMap modelMap)
    {
        return Constants.MODULE_MONITOR_LOG_PREFIX + "log";
    }
    
    /**
     * 列表界面
     * 
     * @param log
     * @return
     */
    @RequiresPermissions("monitor:log:list")
    @PostMapping("/list")
    @ResponseBody
    public ResponseEntity<ModelMap> list(SysLog log)
    {
        SysUser currentUser = ShiroUtils.getCurrentUser();
        if (currentUser.getIsAdmin() != 1) 
        {
            log.setCreateBy(currentUser.getId());
        }
        PageInfo<SysLog> pageInfo = this.sysLogService.selectList(ConvertRequestUtil.pageRequest(), log);
        return setSuccessModelMap(pageInfo);
    }
    
    /**
     * 删除日志
     * 
     * @param ids
     * @return
     */
    @Log(module = Module.LOG, value = "删除日志", type = LogType.DELETE)
    @RequiresPermissions("monitor:log:remove")
    @PostMapping("/remove")
    @ResponseBody
    public ResponseEntity<ModelMap> remove(String ids)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysLogService.removeByIds(Arrays.asList(ConvertUtil.toLongArray(ids)));
        return setSuccessModelMap();
    }
    
    /**
     * 日志详情
     * 
     * @param operId
     * @param mmap
     * @return
     */
    @RequiresPermissions("monitor:log:detail")
    @GetMapping("/detail/{operId}")
    public String detail(@PathVariable("operId") Long operId, ModelMap mmap)
    {
        mmap.put("operLog", this.sysLogService.queryById(operId));
        // 修复服务器部署出现的BUG
        return Constants.MODULE_MONITOR_LOG_PREFIX + "detail";
    }
    
    /**
     * 清空日志
     * 
     * @return
     */
    @Log(module = Module.LOG, value = "清空日志", type = LogType.CLEAN)
    @RequiresPermissions("monitor:log:remove")
    @PostMapping("/clean")
    @ResponseBody
    public ResponseEntity<ModelMap> clean()
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysLogService.deleteByMap(Maps.newHashMap());
        return setSuccessModelMap();
    }
    
}
